import settings from "../utils/settings";
import { DrawString, ServerTick, timedisplay } from "../utils/utils";
import { location } from "../utils/getLocation";
import { guiMover } from "../utils/gui";


function getClassLevel (s) {
    let num = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000 };
    let a = [];
    a = s.split(" ")[s.split(" ").length - 1];
    let b = 0;
    for (let i = 0; i < a.length; i++) {
        if (num[a[i]] < num[a[i + 1]]) {
            b += (num[a[i + 1]] - num[a[i]]);
            i++;
        } else {
            b += num[a[i]];
        }
    }
    return b;
}

function InDungeonCooldown(timenow,cd,nodupe){
    let magelevel = 0;
    let ismage = false;
    TabList.getNames().forEach( (e) => {
        if(e.includes(Player.getName()) && e.includes("Mage")) {
            let a = ChatLib.removeFormatting(e).trim();
            let b = a.replace(")","");
            ismage = true;
            magelevel = getClassLevel(b);
            return;
        }
    })
    if(!ismage) return timenow;
    if(nodupe) return timenow - (25*2+parseInt(magelevel/2))*(cd/100);
    return timenow - (25+parseInt(magelevel/2))*(cd/100);
    
}

export function withercloak(){
let Duration = 0;
let Cooldown = 0;
let inactivated = false;
let alignedtime = 0;
let alignedPlayer = "";
let nodupemage = false;
let GravityStorm = 0;
let SoulwardCooldown = 0;
let SoulwardDuration = 0;

register("worldLoad", () => {
    Duration = 0;
    Cooldown = 0;
    inactivated = false;
    alignedtime = 0;
    alignedPlayer = "";
    nodupemage = false;
    GravityStorm = 0;
    SoulwardCooldown = 0;
})

register("chat", (a,b) => {
    nodupemage = true;
}).setChatCriteria("[Mage] Cooldown Reduction ${a}% -> ${b}%")

register("chat", (message) => {
    if(!settings.WitherCloakTimer && !settings.AlignedTimer){return};
    if (!location.inSkyblock) return;
    switch(message){
        case("Creeper Veil Activated!"):
            if(settings.WitherCloakTimer){
                inactivated = true;
                Cooldown = 0;
                Duration = ServerTick.getTime();
            };
            break;
        case("Creeper Veil De-activated!"):
            if(settings.WitherCloakTimer){
                inactivated = false;
                Duration = 0;
                if(location.inDungeon){
                    Cooldown = InDungeonCooldown((ServerTick.getTime() - 5000),5000,nodupemage)
                }else{Cooldown = ServerTick.getTime() - 5000}
            };
            break;
        case("Not enough mana! Creeper Veil De-activated!"):
        case("Creeper Veil De-activated! (Expired)"):
            if(settings.WitherCloakTimer){
                inactivated = false;
                Duration = 0;
                if(location.inDungeon){
                    Cooldown = InDungeonCooldown(ServerTick.getTime(),10000,nodupemage)
                }else{Cooldown = ServerTick.getTime()}
            };
            break;
        case("You aligned yourself!"):
        case("You aligned 1 other player!"):
        case("You aligned 2 other players!"):
        case("You aligned 3 other players!"):
        case("You aligned 4 other players!"):
            if(settings.AlignedTimer){
                alignedtime = ServerTick.getTime();
                alignedPlayer = "§e§lby §r§ayourself";
            };
            break;
        default:
            break;
    };
}).setCriteria("${message}");

register("chat", (player) => {
    if(!settings.AlignedTimer){return};
    if (location.inSkyblock && !player.includes(' ')) {
        alignedtime = ServerTick.getTime();
        alignedPlayer = "§e§lby §r§b" + player;
    };
    return;
}).setCriteria("${player} casted Cells Alignment on you!");

register("actionbar",(ManaCost) => {
    if(location.inSkyblock && settings.GravityStormCooldown && ServerTick.getTime() - GravityStorm > 29000) {
        if(location.inDungeon) {
            GravityStorm = InDungeonCooldown(ServerTick.getTime(),30000,nodupemage);
        } else {
            GravityStorm = ServerTick.getTime();
        }
    }
}).setCriteria("-${ManaCost} Mana (Gravity Storm)").setParameter("contains");

register("actionbar",(ManaCost) => {
    if(location.inSkyblock && settings.WitherCloakTimer && ServerTick.getTime() - SoulwardCooldown > 19000) {
        if(location.inDungeon) {
            SoulwardCooldown = InDungeonCooldown(ServerTick.getTime(),20000,nodupemage);
        } else {
            SoulwardCooldown = ServerTick.getTime();
        }
        SoulwardDuration = ServerTick.getTime();
    }
}).setCriteria("-${ManaCost} Mana (Soulward)").setParameter("contains");

register("tick",()=>{
    if(!location.inSkyblock || inactivated) return;
    if(ServerTick.getTime() - Cooldown > 10000 && ServerTick.getTime() - Cooldown <= 10050) World.playSound("note.pling", 1, 1.5);
})

register("renderOverlay", () => {
    //edit mode
    if(guiMover.gui.isOpen()){
        if(settings.WitherCloakTimer)
        DrawString("§4§lWither Cloak\n§4cooldown 6.666\n§1§lSoulward§7:§r §a3.550\n§a§laligned §r§61.234 §a|||\n" + (settings.Whoalignedyou ? "§e§lby §r§ayourself\n" : "") + "§5§lGravity Storm §r§b23.456" , guiMover.settings.WitherCloak.x , guiMover.settings.WitherCloak.y , guiMover.settings.WitherCloak.s , true);
        return;
    }
    //wither cloak
    if (!location.inSkyblock) return;
    DrawString(
        (ServerTick.getTime() - Cooldown < 10000 && !inactivated ? "§4§lWither Cloak\n§4cooldown " + timedisplay(10000-(ServerTick.getTime()-Cooldown)) + '\n' : 
        (ServerTick.getTime() - Duration < 10000 && inactivated ? "§a§lWither Cloak\n§aactivated " + timedisplay(10000-(ServerTick.getTime()-Duration)) + '\n' : 
        (ServerTick.getTime() - Cooldown < 12000 && !inactivated ? "§d§lWither Cloak\n§4§l   Ready!\n" : ''))) +
        (ServerTick.getTime() - SoulwardDuration < 5000 || ServerTick.getTime() - SoulwardCooldown < 20000 ? "§1§lSoulward§7:§r " + 
        (ServerTick.getTime() - SoulwardDuration < 5000 ? "§a" + timedisplay(5000-(ServerTick.getTime()-SoulwardDuration)) : "§4" + timedisplay(20000-(ServerTick.getTime()-SoulwardCooldown))) + '\n' : "") +
        (ServerTick.getTime() - alignedtime < 6000 ? "§a§laligned §r§6" + timedisplay(6000-(ServerTick.getTime()-alignedtime)) + " §a|||" + (settings.Whoalignedyou ? '\n' + alignedPlayer : '' ) + '\n' : '') +
        (ServerTick.getTime() - GravityStorm < 30000 ? "§5§lGravity Storm §r§b" + timedisplay(30000-(ServerTick.getTime()-GravityStorm)) : '')
        , guiMover.settings.WitherCloak.x , guiMover.settings.WitherCloak.y , guiMover.settings.WitherCloak.s , true);
})

}
